/*
 * Copyright (C) 2025 Tsingteng MicroSystem
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef _NFC_TYPES_H_
#define _NFC_TYPES_H_

#include <stdint.h>
#include <stddef.h>

#ifndef unlikely
#define unlikely(x)     __builtin_expect(!!(x), 0)
#endif

#define OSI_MAX_ALLOC_SIZE (1024 * 1024 * 5)
#define OSI_ARRAY_SIZE(arr)    (sizeof(arr) / sizeof((arr)[0]))
#define OSI_CHK_PARAM_INVALID(check, statement)           \
    if (unlikely(check)) {                                \
        NFCLOG_E("Invalid param, unexpected: %s", #check); \
        do { statement; } while (0);                      \
    }

#define NFC_CONFIG_PATHS(file) \
{ \
    "./" file, \
    "/odm/etc/" file, \
    "/vendor/etc/" file, \
    "/etc/" file, \
    "/data/vendor/nfc/" file \
}

typedef enum {
    // 0-4对应HIDL/AIDL中定义的状态码
    NFC_STATUS_OK = 0,
    NFC_STATUS_FAILED = 1,
    NFC_STATUS_ERR_TRANSPORT = 2,
    NFC_STATUS_ERR_CMD_TIMEOUT = 3,
    NFC_STATUS_REFUSED = 4,
    // 下面是扩展的错误码, 从0x10开始
    NFC_STATUS_TIMEOUT_RSP = 0x10,
    NFC_STATUS_TIMEOUT_NTF,
    NFC_STATUS_INVALID_PARAM,
    NFC_STATUS_TIMEOUT,
    NFC_STATUS_NOT_INIT,
    NFC_STATUS_OPEN_FILE_ERROR,
    NFC_STATUS_FILE_FORMAT_ERROR,
    NFC_STATUS_GET_LOCK_FAILED,
} tNFC_STATUS;

typedef struct {
    size_t cap;     // 已经分配的内存大小（不包含 cap和len）
    size_t len;     // 实际使用的长度（不包含cap和len）
    uint8_t dat[0]; // 保存数据的起始地址
} tARRAY;

/**
 * @brief NFC配置数组结构体，用于存储数组类型的NFC配置
*/
typedef struct {
    union {
        const uint8_t *data;
        uint8_t *inner_data; /* 仅config模块内部操作使用，外部统一使用data获取 */
    };
    size_t len;
} tNFC_CF_ARRAY;

#endif /* _NFC_TYPES_H_ */
